package com.soo.hoo.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soo.hoo.admin.model.entity.SysBookReciprocation;
import com.soo.hoo.admin.service.BookReceivingService;
import com.soo.hoo.admin.service.BookReciprocationService;
import com.soo.hoo.admin.service.BookService;
import com.soo.hoo.core.annotation.OperateLog;
import com.soo.hoo.core.enums.BusinessTypeEnum;
import com.soo.hoo.core.exception.GlobalException;
import com.soo.hoo.core.model.Page;
import com.soo.hoo.core.model.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 还礼管理
 * </p>
 *
 * @author 杨燚
 * @since 2024/4/29
 */
@Tag(name = "还礼管理")
@RequestMapping("/reciprocation/")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ReciprocationController {

    private final BookReceivingService receivingService;
    private final BookReciprocationService reciprocationService;


    @PostMapping("create")
    @Operation(summary = "创建回礼记录")
    @OperateLog(title = "创建回礼记录", businessType = BusinessTypeEnum.CREATE)
    public Result<Void> create(@RequestBody @Valid SysBookReciprocation reciprocation) {
        boolean saved = reciprocationService.save(reciprocation);
        if (saved) {
            receivingService.updateReciprocated(reciprocation.getReceivingId(), true);
        }
        return Result.success();
    }

    @Operation(summary = "列表")
    @GetMapping("list")
    @OperateLog(title = "回礼列表", businessType = BusinessTypeEnum.QUERY)
    public Result<List<SysBookReciprocation>> list(@RequestParam Long receivingId) {
        List<SysBookReciprocation> list = reciprocationService.list(
                new LambdaQueryWrapper<SysBookReciprocation>()
                        .eq(SysBookReciprocation::getReceivingId, receivingId)
        );
        return Result.success(list);
    }

    @PostMapping("update")
    @Operation(summary = "修改回礼记录")
    @OperateLog(title = "修改回礼记录", businessType = BusinessTypeEnum.UPDATE)
    public Result<Void> update(@RequestBody @Valid SysBookReciprocation reciprocation) {
        Long recId = Optional.of(reciprocation.getId()).orElseThrow(
                () -> new GlobalException("修改失败，未找到回礼记录，请联系管理员")
        );
        SysBookReciprocation rec = reciprocationService.getById(recId);
        if (rec == null) {
            throw new GlobalException("修改失败，未找到回礼记录，请联系管理员");
        }
        reciprocationService.updateById(reciprocation);
        return Result.success();
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除回礼记录")
    @OperateLog(title = "删除回礼记录", businessType = BusinessTypeEnum.DELETE)
    public Result<Void> delete(@RequestParam Long id) {
        SysBookReciprocation rec = reciprocationService.getById(id);
        reciprocationService.removeById(id);
        List<SysBookReciprocation> list = reciprocationService.list(
                new LambdaQueryWrapper<SysBookReciprocation>()
                        .eq(SysBookReciprocation::getReceivingId, rec.getReceivingId())
        );
        receivingService.updateReciprocated(rec.getReceivingId(), list);
        return Result.success();
    }
}
